import vue from "@vitejs/plugin-vue";
import createVitePlugins from "./vite/plugins";
import { defineConfig, loadEnv } from "vite";
import path from "path";

// https://vite.dev/config/
export default defineConfig((mode, command) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;

  return {
    base: "/",

    plugins: createVitePlugins(env, command === "build"),

    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        "~": path.resolve(__dirname, "./"),
        // 设置别名
        "@": path.resolve(__dirname, "./src"),
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    server: {
      port: 5174,
      // host: true,
      proxy: {
        "/api": {
          target: "http://47.93.174.47:8080",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    build: {
      target: "es2015",
      assetsInlineLimit: 40960, // 图片转 base64 编码的阈值 单位为字节 40960 = 40kb
      modulePreload: {
        // 预加载
        polyfill: false, // 这个没用
        resolveDependencies: () => [],
      },
      rollupOptions: {
        inlineDynamicImports: true, // 内联动态导入
        output: {
          // manualChunks: {
          //   // "vue-vendor": ["vue", 'vue-router','vuex',  ],
          //   // "echarts-wordcloud": ["echarts-wordcloud" ],
          //   // "vue-echarts":["vue-echarts",],

          //   // "axios":["axios",],
          //   // vendor: [],

          //   // "ant-design-vue": ["ant-design-vue"],
          //   "echarts": ["echarts"],
          //   // 'pdf-vender':['jspdf','html2canvas'],
          //   // "pdf-vender": ["jspdf"],
          // },
          // manualChunks(id) {
          //   console.log("id", id);
          //   if (id.includes("node_modules")) {
          //     const modulePath = id.split("node_modules/")[1];
          //     const topLevelFolder = modulePath.split("/")[0];
          //     if (topLevelFolder !== ".pnpm") {
          //       return topLevelFolder;
          //     }
          //     const scopedPackageName = modulePath?.split("/")[1];
          //     const chunkName = scopedPackageName?.split("@")[scopedPackageName.startsWith("@") ? 1 : 0];
          //     return chunkName;
          //   }
          // },
        },
      },
    },
  };
});
